<template>
  <el-drawer
    v-model="drawerVisible"
    title="购物车"
    direction="rtl"
    size="400px"
    :before-close="handleClose"
  >
    <div class="cart-content">
      <div v-if="cartStore.items.length === 0" class="empty-cart">
        <el-empty description="购物车是空的" />
      </div>
      <div v-else class="cart-items">
        <div v-for="item in cartStore.items" :key="item.id" class="cart-item">
          <el-image
            :src="item.image"
            fit="cover"
            class="item-image"
          />
          <div class="item-info">
            <div class="item-name">{{ item.name }}</div>
            <div class="item-price">¥{{ item.price }}</div>
            <div class="item-quantity">
              <el-input-number
                v-model="item.quantity"
                :min="1"
                :max="99"
                size="small"
                @change="(value) => updateQuantity(item.id, value)"
              />
            </div>
          </div>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            size="small"
            @click="removeItem(item.id)"
          />
        </div>
      </div>
    </div>
    <template #footer>
      <div class="cart-footer">
        <div class="total-price">
          总计: ¥{{ cartStore.cartTotal }}
        </div>
        <el-button
          type="primary"
          :disabled="cartStore.items.length === 0"
          @click="checkout"
        >
          结算
        </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup>
import { ref, defineProps, defineEmits, watch } from 'vue'
import { Delete } from '@element-plus/icons-vue'
import { useCartStore } from '@/store/modules/cart'
import { ElMessage } from 'element-plus'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'checkout'])

const drawerVisible = ref(props.visible)
const cartStore = useCartStore()

// 监听 visible 属性变化
watch(() => props.visible, (newVal) => {
  drawerVisible.value = newVal
})

// 监听 drawerVisible 变化并通知父组件
watch(drawerVisible, (newVal) => {
  emit('update:visible', newVal)
})

const handleClose = (done) => {
  done()
}

const updateQuantity = (itemId, quantity) => {
  cartStore.updateQuantity(itemId, quantity)
}

const removeItem = (itemId) => {
  cartStore.removeItem(itemId)
  ElMessage.success('已从购物车移除')
}

const checkout = () => {
  emit('checkout')
}
</script>

<style scoped>
.cart-content {
  padding: 20px;
  height: calc(100% - 100px);
  overflow-y: auto;
}

.empty-cart {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.cart-items {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.cart-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px;
  border-bottom: 1px solid #eee;
}

.item-image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
}

.item-info {
  flex: 1;
}

.item-name {
  font-size: 14px;
  margin-bottom: 5px;
}

.item-price {
  color: #f56c6c;
  font-weight: bold;
  margin-bottom: 5px;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-top: 1px solid #eee;
}

.total-price {
  font-size: 18px;
  font-weight: bold;
  color: #f56c6c;
}
</style> 